<?php

class Silk_Mapi_Model_Api2_Info_Rest extends Silk_Mapi_Model_Api2_Info
{
    public function _retrieve()
    {
        $cms = Mage::getModel('cms/page')->load('privacy-policy', 'identifier');
        if ($cms && $cms->getId()) {
            $content = $cms->getContent();
            $content = Mage::helper('cms')->getBlockTemplateProcessor()->filter($content);
        } else {
            $content = '';
        }

        return array(
            'content' => $content
        );
    }

    public function _retrieveCollection()
    {
        /** @var Mage_Core_Model_Mysql4_Store_Collection $storeCollection */
        $storeCollection = Mage::getModel('core/store')->getCollection()
            ->addFieldToFilter('is_active', 1);

        $regionIds = array();
        $cityIds = array();
        $areaIds = array();
        foreach ($storeCollection as $store) {
            $website = Mage::getSingleton('core/website')->load($store->getWebsiteId(), 'website_id');
            if ($website && $website->getId()) {
                $regionIds[] = $website->getProvinceId();
                $cityIds[] = $website->getCityId();
                $areaIds[] = $website->getAreaId();
            }
        }

        return $this->getAreas($regionIds, $cityIds, $areaIds);
    }

    protected function getAreas($regionIds, $cityIds, $areaIds)
    {
        $regionIds = $this->formatIds($regionIds);
        $cityIds = $this->formatIds($cityIds);
        $areaIds = $this->formatIds($areaIds);
        /** @var Varien_Db_Adapter_Interface $db */
        $db = Mage::getSingleton('core/resource')->getConnection('core_read');
        $sql = "SELECT dcr.region_id, dcr.default_name region_name,dcc.city_id, dcc.default_name city_name, dcc.region_id city_parent_id,dca.area_id, dca.default_name area_name, dca.city_id area_parent_id, cw.website_id
FROM directory_country_region dcr
LEFT JOIN directory_country_city dcc ON dcr.region_id=dcc.region_id
LEFT JOIN directory_country_area dca ON dcc.city_id=dca.city_id
LEFT JOIN core_website cw ON dca.area_id=cw.area_id
WHERE dcr.region_id IN ({$regionIds}) AND dcc.city_id IN ({$cityIds}) AND dca.area_id IN ({$areaIds}) AND cw.website_id NOT IN (0,1);";

        $query = $db->query($sql);
        $areaInfo = $query->fetchAll();
        $areas = array();
        foreach ($areaInfo as $info) {
            $areas['region'][$info['region_id']] = array(
                'id' => (int)$info['region_id'],
                'name' => $info['region_name'],
                'parent_id' => 0
            );
            $areas['city'][$info['city_id']] = array(
                'id' => (int)$info['city_id'],
                'name' => $info['city_name'],
                'parent_id' => (int)$info['city_parent_id']
            );
            $areas['area'][$info['area_id']] = array(
                'id' => (int)$info['website_id'],
                'name' => $info['area_name'],
                'parent_id' => (int)$info['area_parent_id']
            );
        }

        $areas['region'] = array_values($areas['region']);
        $areas['city'] = array_values($areas['city']);
        $areas['area'] = array_values($areas['area']);

        return $areas;
    }

    protected function formatIds($ids)
    {
        if (is_array($ids)) {
            return implode(',', array_unique($ids));
        }
        return $ids;
    }
}